/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hd.assistant.web.home.module.screen.order;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.hd.assistant.dal.dao.query.ordermining.TcOrderQuery;
import com.hd.assistant.dal.dataobject.User;
import com.hd.assistant.dal.dataobject.ordermining.TcOrder;
import com.hd.assistant.web.common.base.ordermining.BaseTaobaoOrderModule;
import com.hd.util.CommonUtil;
/**
 *
 * @author Hangding
 */
public class Trades extends BaseTaobaoOrderModule{
    private final static Logger log = LoggerFactory.getLogger(Trades.class);
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpSession session;
    
    
   
    public void execute(
    		@Param("page") Integer page,
    		@Param("pageSize") Integer pageSize,
    		@Param("tid") Long tid,
    		@Param("buyerNick") String buyerNick,
    		@Param("status") String status,
    		@Param("payTimeStart") String payTimeStart,
    		@Param("payTimeEnd") String payTimeEnd,
    		@Param("endTimeStart") String endTimeStart,
    		@Param("endTimeEnd") String endTimeEnd,
    		@Param("isProfitCal") Integer isProfitCal,
    		@Param("noProfit") Boolean noProfit,
    		@Param("profitStart") Double profitStart,
    		@Param("profitEnd") Double profitEnd,
    		Context context, Navigator nav) {
    	try{
    		User u = checkPermision(User.Type_TAOBAO_ORDER, session, context, nav);
    		if(u == null)
    			return;
    		if(pageSize == null)
    			pageSize = 10;
    		if(page == null)
    			page = 1;
    		TcOrderQuery query = new TcOrderQuery();
    		query.setUserId(u.getUserId());

    		query.setPage(page);
    		query.setPageSize(pageSize);
    		if(tid != null)
    			query.setTid(tid);
    		if(buyerNick != null){
    			query.setBuyerNick(buyerNick);
    		}
    		if(status != null){
    			if(!status.equals("all")){
    				List<String> statuses = new ArrayList<String>();
    				if(status.equals("1")){
    					query.setStatus(TcOrder.WAIT_BUYER_PAY);
    				}
    				if(status.equals("2")){
    					//表示已付款
    					statuses.add(TcOrder.WAIT_SELLER_SEND_GOODS);
    					statuses.add(TcOrder.WAIT_BUYER_CONFIRM_GOODS);
    					statuses.add(TcOrder.TRADE_BUYER_SIGNED);
    					query.setStatuses(statuses);
    				}
    				if(status.equals("3")){
    					//表示交易成功
    					query.setStatus(TcOrder.TRADE_FINISHED);
    				}
    				if(status.equals("4")){
    					//表示应计算利润状态
    					statuses.add(TcOrder.WAIT_SELLER_SEND_GOODS);
    					statuses.add(TcOrder.WAIT_BUYER_CONFIRM_GOODS);
    					statuses.add(TcOrder.TRADE_BUYER_SIGNED);
    					statuses.add(TcOrder.TRADE_FINISHED);
    					query.setStatuses(statuses);
    				}
    				
    			}
    		}
    		
    		if(payTimeStart != null)
    			query.setPayTimeStart(CommonUtil.parseDate("yyyyMMdd",payTimeStart));
    		
    		if(payTimeEnd != null)
    			query.setPayTimeEnd(CommonUtil.parseDate("yyyyMMdd",payTimeEnd));
    		
    		if(endTimeStart != null)
    			query.setEndTimeStart(CommonUtil.parseDate("yyyyMMdd",endTimeStart));
    		
    		if(endTimeEnd != null)
    			query.setEndTimeEnd(CommonUtil.parseDate("yyyyMMdd",endTimeEnd));
    		
    		if(isProfitCal != null)
    			query.setIsProfitCal(isProfitCal);
    		
    		if(profitStart != null)
    			query.setProfitStart(profitStart);
    		
    		if(profitEnd != null){
    			query.setProfitEnd(profitEnd);
    		}
    		
    		if(noProfit != null)
    			query.setNoProfit(noProfit);
    		
    		
    		query = this.orderMiningManager.getTcOrdersByQuery(query);
    		context.put("result", query);
    		context.put("pindex", 1);
    		context.put("index", 101);
    		query.setStatus(status);//设计不好 以后改  前段和数据库用了1个字段 不同意义的字段
    	}catch(Exception e){
    		log.error("error Itemcosts ",e);
    	}
	    	
    }
	
}
